import os
import unittest

import ext4.ext4 as ext4


class Testle(unittest.TestCase):
    def test_le16(self):
        self.assertEqual(ext4.le16(b'\x23\x01').val, 0x123)
    
    def test_le32(self):
        self.assertEqual(ext4.le32(b'\x67\x45\x23\x01').val, 0x1234567)
    
    def test_le64(self):
        self.assertEqual(ext4.le64(b'\x67\x45\x23\x01\x67\x45\x23\x01').val, 0x123456701234567)


class TestExt4(unittest.TestCase):
    def __init__(self, methodName: str = "runTest") -> None:
        super().__init__(methodName)

        ext4_img = os.path.join(__path__, "ext4.img")
        if not os.access(ext4_img, os.F_OK):
            os.system("dd if=/dev/zero of=%s bs=1024 count=1024" % ext4_img)
            os.system("mkfs.ext4 %s" % ext4_img)
        
            ext4_dir = os.path.join(__path__, 'ext4_mount_dir')
            os.mkdir(ext4_dir)
            os.system("mount %s %s" % (ext4_img, ext4_dir))


            ext4_file = os.path.join(ext4_dir, 'test.txt')
            with open(ext4_file, 'w') as f:
                f.write('hello world')
            
            os.system("umount %s" % ext4_dir)

        self.ext4 = ext4.Ext4(ext4_img)

    def test_surper_block(self):
        pass


if __name__ == "__main__":
    unittest.main()